<?php
namespace Orm\Staff;

use Base\Exception\RmException;
class StaffClass extends \Base\BaseOrmObject{
    protected   $id,
				$account = '', // 账户
				$pwd = '', // 密码
				$name = '', // 姓名
				$tel = '', // 手机号
				$email = '', // 邮箱
				$admin_id = 0, // 创建者ID
				$enterprise_role_id = 0, // 角色ID
				$country = 0, // 国家id
				$province = 0, // 省份id
				$city = 0, // 城市id
				$area = 0, // 区域id
				$address = '', // 详细地址
				$type = 0, // 类型 2:用户 1:客户
				$remark = '', // 备注
				$is_del = 0, // 0正常 1删除
				$is_use = 1, // 1: 使用 0: 未使用
				$insert_time = '', // 创建时间
				$update_time = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Staff\StaffModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'account' => $this->account,
			'pwd' => $this->pwd,
			'name' => $this->name,
			'tel' => $this->tel,
			'email' => $this->email,
			'admin_id' => $this->admin_id,
			'enterprise_role_id' => $this->enterprise_role_id,
			'country' => $this->country,
			'province' => $this->province,
			'city' => $this->city,
			'area' => $this->area,
			'address' => $this->address,
			'type' => $this->type,
			'remark' => $this->remark,
			'is_use' => $this->is_use,
        );

        $model = new \Orm\Staff\StaffModel();
        $model->insert($data);
        $this->id = $model->getLastId();
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '新增', $this->backup_data);
        /******************************************************/
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'account' => $this->account,
			'pwd' => $this->pwd,
			'name' => $this->name,
			'tel' => $this->tel,
			'email' => $this->email,
			'admin_id' => $this->admin_id,
			'enterprise_role_id' => $this->enterprise_role_id,
			'country' => $this->country,
			'province' => $this->province,
			'city' => $this->city,
			'area' => $this->area,
			'address' => $this->address,
			'type' => $this->type,
			'remark' => $this->remark,
			'is_use' => $this->is_use,
        );

        $model = new \Orm\Staff\StaffModel();
        $model->update($data, "`id` = {$this->id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '更新', $this->backup_data);
        /******************************************************/
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Staff\StaffModel();
        $model->update($data, "`id` = {$id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '删除', $this->backup_data);
        /******************************************************/
    }

    private function checkNecessaryParam()
    {
    }
}